import store from '../store'; 
import Cache from '../utils/cache';
import {appId} from '@/config/app.js'
import { STATE_R_KEY, USER_INFO, EXPIRES_TIME, LOGIN_STATUS} from './../config/cache';
class Routine 
{ 
	constructor() 
	{
	    this.scopeUserInfo = 'scope.userInfo';
	}
	
	async getUserCode(){
		let code= await this.getCode();
		return code;
	}
	

	
	/**
	 * 获取用户信息
	 */
	async getUserInfo(){
		let  that = this ;
		let code =await  this.getUserCode(); 
		return new Promise( (resolve,reject) => {
			// // #ifdef MP-WEIXIN
			// 	uni.getUserProfile({ 
			// 		desc:'显示用户头像昵称',
			// 		success(user) {
			// 			console.log("user====================",user);
			// 			if(code) user.code = code;
			// 			resolve({userInfo:user,islogin:false});
			// 		},
			// 		fail(res){ 
			// 			reject(res);
			// 		}
			// 	})
			// // #endif 
			
				uni.getUserInfo({
					lang: 'zh_CN', 
					success(user) { 
						if(code) user.code = code;
						resolve({userInfo:user,islogin:false});
					},
					fail(res){ 
						reject(res);
					}
				}) 
		})
	} 
	
	async getCode(){
		let provider = await this.getProvider();
		return new Promise((resolve,reject)=>{ 
			uni.login({
				provider:provider,
				success(res) {
					if (res.code) Cache.set(STATE_R_KEY, res.code ,10800);
					return resolve(res.code);
				},
				fail(){
					return reject(null);
				}
			})
		})
	}  
	/**
	 * 获取服务供应商
	 */
	getProvider()
	{
		return new Promise((resolve,reject)=>{
			uni.getProvider({
				service:'oauth',
				success(res) {
					resolve(res.provider);
				},
				fail() {
					resolve(false);
				}
			});
		});
	} 
}

export default new Routine();